package tour.spring.web.controller;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import tour.spring.bc.bo.impl.HotelBoImpl;
import tour.spring.bc.bo.impl.TourBoImpl;
import tour.spring.bc.dto.HotelDto;
import tour.spring.web.commandobject.AddTourForm;
import tour.spring.web.commandobject.HotelRESTInfo;

@Controller
public class AdminController {

	@Autowired
	HotelBoImpl hotelBo;

	@Autowired
	TourBoImpl tourBo;

	@InitBinder
	public void initBinder(WebDataBinder binder) {

		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}

	@RequestMapping(value = "admin/addTour.html", method = RequestMethod.GET)
	public String getAddTourPage(HttpServletRequest request, ModelMap model) { // request chyba niepotrzebny

		Collection<HotelDto> hotels = hotelBo.queryAll();
		model.addAttribute("hotels", hotels);
		
		AddTourForm addTourForm = new AddTourForm();
		model.addAttribute("addTourForm", addTourForm);
		
		return "admin/addTourPage";
	}

	@RequestMapping(value = "admin/addTour.html", method = RequestMethod.POST)
	public String getAddTourPageResult(
			@ModelAttribute("addTourForm") AddTourForm addTourForm,
			ModelMap model) {

		tourBo.saveTour(addTourForm.getDateFrom(), addTourForm.getDateTo(),
				addTourForm.getHotelId(), addTourForm.getPlaceQuantity(),
				addTourForm.getBasePrice(), addTourForm.getDescription());
		return "admin/addTourComplete";
	}
	
	
	@RequestMapping(value = "/placesInHotel.html", method = RequestMethod.POST)
	public @ResponseBody int getPlaceInHotel(@RequestParam(value="hotelId") int hotelId, ModelMap model) {

		RestTemplate rest = new RestTemplate();
		HotelRESTInfo hotelInfo = rest.getForObject("http://localhost:8080/RESTService/hotel/{hotelId}", HotelRESTInfo.class, hotelId);
		return hotelInfo.getPlaceQuantity();
	}
	

	public HotelBoImpl getHotelBo() {
		return hotelBo;
	}

	public void setHotelBo(HotelBoImpl hotelBo) {
		this.hotelBo = hotelBo;
	}

	public TourBoImpl getTourBo() {
		return tourBo;
	}

	public void setTourBo(TourBoImpl tourBo) {
		this.tourBo = tourBo;
	}

}
